package com.hc.testaudio.deletea.test1.chart;

import android.content.Context;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class BarChartUtil {
    private BarChart mBarChart;

    public BarChartUtil(BarChart barChart, Context context) {
        this.mBarChart = barChart;
        mBarChart.setNoDataText("");
//        mBarChart.setOnChartValueSelectedListener(this);//设置点击事件

        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawValueAboveBar(true);
        mBarChart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // 设置启用setDrawValues（）时，图表上最大可见绘制值的数量仅处于活动状态*
        mBarChart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        mBarChart.setPinchZoom(false);
        mBarChart.setDrawGridBackground(false);//是否绘制网格背景

        //底部x轴配置
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴标签的位置
//        xAxis.setTypeface(tfLight);//设置特定字体
        xAxis.setDrawGridLines(false);//x轴是否绘制网格线
        xAxis.setGranularity(1f); // x轴最小间距
//        xAxis.setLabelCount(7);
        xAxis.setDrawAxisLine(false);//是否绘制延轴线
//        xAxis.setValueFormatter(ValueFormatter);//可以用于自定义x轴格式

        //左侧y轴配置
        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
//        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);//将顶部轴空间设置为整体的百分比。默认10f（即10%）
//        leftAxis.setAxisMinimum(0f); //通过调用此方法，先前设置的任何自定义最小值将被重置，并自动完成计算。 this replaces setStartAtZero(true)
        mBarChart.setMarker(new ChartMarker(context));
    }

    public void add(List<Float> yDataList) {
        List<BarEntry> dataEntry = new ArrayList<>();
        int length = yDataList.size();
        for (int i = 0; i < length; i++) {
            dataEntry.add(new BarEntry(i, yDataList.get(i)));
        }
        BarDataSet dataSets;
        if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) {
            dataSets = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            dataSets.setValues(dataEntry);
            mBarChart.getData().notifyDataChanged();
            mBarChart.notifyDataSetChanged();
        } else {
            dataSets = new BarDataSet(dataEntry, "");
            BarData barData = new BarData(dataSets);
            mBarChart.setData(barData);
        }
    }

    public void add(float[] xDataList, float[] yDataList) {
        List<BarEntry> dataEntry = new ArrayList<>();
        int length = xDataList.length > yDataList.length ? yDataList.length : xDataList.length;
        for (int i = 0; i < length; i++) {
            dataEntry.add(new BarEntry(xDataList[i], yDataList[i]));
        }
        BarDataSet dataSets;
        if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) {
            dataSets = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            dataSets.setValues(dataEntry);
            mBarChart.getData().notifyDataChanged();
            mBarChart.notifyDataSetChanged();
        } else {
            dataSets = new BarDataSet(dataEntry, "");
            BarData barData = new BarData(dataSets);
            mBarChart.setData(barData);
        }
    }

    public void add(List<Float> xDataList, List<Float> yDataList) {

        List<BarEntry> dataEntry = new ArrayList<>();
        int length = xDataList.size() > yDataList.size() ? yDataList.size() : xDataList.size();
        for (int i = 0; i < length; i++) {
            dataEntry.add(new BarEntry(xDataList.get(i), yDataList.get(i)));
        }
        BarDataSet dataSets;
        if (mBarChart.getData() != null && mBarChart.getData().getDataSetCount() > 0) {
            dataSets = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            dataSets.setValues(dataEntry);
            mBarChart.getData().notifyDataChanged();
            mBarChart.notifyDataSetChanged();
        } else {
            dataSets = new BarDataSet(dataEntry, "");
            BarData barData = new BarData(dataSets);
            mBarChart.setData(barData);
        }
    }

    public void invalidate() {
        mBarChart.invalidate();
    }
}
